package com.xinzhidi.web.householdClassify.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinzhidi.common.core.domain.model.LoginUser;
import com.xinzhidi.common.core.page.TableDataInfo;
import com.xinzhidi.common.core.text.Convert;
import com.xinzhidi.common.exception.ServiceException;
import com.xinzhidi.common.utils.DateUtils;
import com.xinzhidi.common.utils.SecurityUtils;
import com.xinzhidi.common.utils.StringUtils;
import com.xinzhidi.common.utils.bean.BeanUtils;
import com.xinzhidi.system.service.ISysUserService;
import com.xinzhidi.web.house.domain.Household;
import com.xinzhidi.web.house.service.IHouseholdService;
import com.xinzhidi.web.householdClassify.domain.HouseholdClassify;
import com.xinzhidi.web.householdClassify.dto.HouseholdClassifyAddDto;
import com.xinzhidi.web.householdClassify.dto.HouseholdClassifyListDto;
import com.xinzhidi.web.householdClassify.dto.HouseholdClassifyUpdDto;
import com.xinzhidi.web.householdClassify.mapper.HouseholdClassifyMapper;
import com.xinzhidi.web.householdClassify.service.IHouseholdClassifyService;
import com.xinzhidi.web.householdClassify.vo.HouseholdClassifyInfoVo;
import com.xinzhidi.web.householdClassify.vo.HouseholdClassifyListVo;
import com.xinzhidi.web.plot.domain.entity.CPlot;
import com.xinzhidi.web.plot.domain.vo.SelectVo;
import com.xinzhidi.web.plot.mapper.CPlotMapper;
import com.xinzhidi.web.plot.service.ICPlotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;



/**
 * 住户分类Service业务层处理
 *
 * @author gm
 * @date 2023-06-09
 */
@Service
public class HouseholdClassifyServiceImpl extends ServiceImpl<HouseholdClassifyMapper, HouseholdClassify>  implements IHouseholdClassifyService {
    @Autowired
    private HouseholdClassifyMapper householdClassifyMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ICPlotService plotService;

    @Autowired
    private CPlotMapper cPlotMapper;

    @Autowired
    private IHouseholdService householdService;

    /**
     * 查询住户分类
     *
     * @param id 住户分类主键
     * @return 住户分类
     */
    @Override
    public HouseholdClassifyInfoVo selectHouseholdClassifyById(Long id){
        HouseholdClassify householdClassify = householdClassifyMapper.selectById(id);
        HouseholdClassifyInfoVo infoVo = new HouseholdClassifyInfoVo();
        BeanUtils.copyProperties(householdClassify,infoVo);
        infoVo.setPlotId(householdClassify.getPlotId().toString());
        return infoVo;
    }

    /**
     * 查询住户分类列表
     *
     * @param dto 查询住户分类dto
     * @return 住户分类
     */
    @Override
    public TableDataInfo<HouseholdClassifyListVo> selectHouseholdClassifyList(HouseholdClassifyListDto dto){
        LambdaQueryWrapper<HouseholdClassify> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(dto.getPlotId() != null,HouseholdClassify::getPlotId,dto.getPlotId());
        wrapper.like(StringUtils.isNotEmpty(dto.getPlotName()),HouseholdClassify::getPlotName,dto.getPlotName());
        wrapper.like(StringUtils.isNotEmpty(dto.getClassifyName()),HouseholdClassify::getClassifyName,dto.getClassifyName());
        if (CollectionUtils.isNotEmpty(dto.getCreateTime()) && dto.getCreateTime().size() > 1 ){
            wrapper.ge(HouseholdClassify::getCreateTime,dto.getCreateTime().get(0)+" 00:00:00");
            wrapper.le(HouseholdClassify::getCreateTime,dto.getCreateTime().get(1)+" 23:59:59");
        }
        wrapper.orderByDesc(HouseholdClassify::getId);
        List<HouseholdClassify> householdClassifies = householdClassifyMapper.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(householdClassifies)){
            Map<Long, String> idMapName = plotService.list(new LambdaQueryWrapper<>(CPlot.class).in(CPlot::getId, householdClassifies.stream().map(HouseholdClassify::getPlotId).collect(Collectors.toList())))
                    .stream().collect(Collectors.toMap(CPlot::getId, CPlot::getParkName));
            return TableDataInfo.getDataTable(householdClassifies,( domain ->{
                HouseholdClassifyListVo vo = new HouseholdClassifyListVo();
                BeanUtils.copyProperties(domain,vo);
                vo.setCreateName(sysUserService.selectUserById(Convert.toLong(domain.getCreateBy())).getNickName());
                vo.setPlotName(idMapName.get(vo.getPlotId()));
                return vo;
            }));
        }else{
            return TableDataInfo.getDataTable(householdClassifies,( domain ->{return null;}));
        }
    }

    /**
     * 查询住户分类列表
     *
     * @param dto 查询住户分类dto
     * @return 住户分类
     */
    @Override
    public TableDataInfo<SelectVo> listHouseholdClassifySelect(HouseholdClassifyListDto dto){
        LambdaQueryWrapper<HouseholdClassify> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(dto.getPlotId() != null,HouseholdClassify::getPlotId,dto.getPlotId());
        wrapper.like(StringUtils.isNotEmpty(dto.getPlotName()),HouseholdClassify::getPlotName,dto.getPlotName());
        wrapper.like(StringUtils.isNotEmpty(dto.getClassifyName()),HouseholdClassify::getClassifyName,dto.getClassifyName());
        if (CollectionUtils.isNotEmpty(dto.getCreateTime())){
            wrapper.ge(HouseholdClassify::getCreateTime,dto.getCreateTime().get(0)+" 00:00:00");
            wrapper.le(HouseholdClassify::getCreateTime,dto.getCreateTime().get(1)+" 23:59:59");
        }
        wrapper.orderByDesc(HouseholdClassify::getId);
        return TableDataInfo.getDataTable(householdClassifyMapper.selectList(wrapper),( domain ->{
            SelectVo vo = new SelectVo();
            vo.setValue(domain.getId().toString());
            vo.setLabel(domain.getClassifyName());
            return vo;
        }));
    }

    @Override
    public boolean moveHouseholdClassify(Long id) {
        Household household = householdService.getById(id);
        household.setType("");
        return householdService.updateById(household);
    }

    /**
     * 新增住户分类
     *
     * @param dto 新增住户分类dto
     * @return 结果
     */
    @Override
    public int insertHouseholdClassify(HouseholdClassifyAddDto dto){
        if (org.apache.commons.collections.CollectionUtils.isNotEmpty(list(new LambdaQueryWrapper<>(HouseholdClassify.class).eq(HouseholdClassify::getClassifyName,dto.getClassifyName()).eq(HouseholdClassify::getPlotId,dto.getPlotId())))){
            throw new ServiceException(dto.getClassifyName()+"已存在！");
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();

        HouseholdClassify householdClassify = new HouseholdClassify();
        BeanUtils.copyProperties(dto,householdClassify);
        householdClassify.setCreateBy(loginUser.getUserId().toString());
        householdClassify.setCreateTime(DateUtils.getNowDate());
        CPlot cPlot = cPlotMapper.selectById(dto.getPlotId());
        householdClassify.setPlotName(cPlot.getParkName());
        return householdClassifyMapper.insert(householdClassify);
    }

    /**
     * 修改住户分类
     * 
     * @param dto 修改住户分类dto
     * @return 结果
     */
    @Override
    public int updateHouseholdClassify(HouseholdClassifyUpdDto dto){
        if (org.apache.commons.collections.CollectionUtils.isNotEmpty(list(new LambdaQueryWrapper<>(HouseholdClassify.class).eq(HouseholdClassify::getClassifyName,dto.getClassifyName()).eq(HouseholdClassify::getPlotId,dto.getPlotId()).ne(HouseholdClassify::getId,dto.getId())))){
            throw new ServiceException(dto.getClassifyName()+"已存在！");
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();

        HouseholdClassify householdClassify = new HouseholdClassify();
        BeanUtils.copyProperties(dto,householdClassify);
        householdClassify.setUpdateBy(loginUser.getUserId().toString());
        householdClassify.setUpdateTime(DateUtils.getNowDate());
        return householdClassifyMapper.updateById(householdClassify);
    }

    /**
     * 批量删除住户分类
     *
     * @param ids 需要删除的住户分类主键
     * @return 结果
     */
    @Override
    public int deleteHouseholdClassifyByIds(Long[] ids){
        return householdClassifyMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除住户分类信息
     * 
     * @param id 住户分类主键
     * @return 结果
     */
    @Override
    public int deleteHouseholdClassifyById(Long id){
        return householdClassifyMapper.deleteById(id);
    }
}
